<template>
  <div class="comment-manage-page">
    <!-- 搜索与筛选区域 -->
    <div class="search-filter-area">
      <el-input
          v-model="commentQuery.content"
          placeholder="评论内容模糊搜索"
          prefix-icon="Search"
          class="search-input"
          @keyup.enter.native="fetchComments"
      />
      <el-select
          v-model="commentQuery.type"
          placeholder="评论类型"
          class="type-select"
          @change="fetchComments"
      >
        <el-option label="全部" value="" />
        <el-option label="歌曲评论" value="0" />
        <el-option label="歌单评论" value="1" />
      </el-select>
      <el-button type="primary" @click="fetchComments">查询</el-button>
      <el-button type="danger" @click="showDeleteConfirm" :disabled="selectedCommentIds.length === 0">批量删除</el-button>
    </div>

    <!-- 评论列表表格 -->
    <el-table
        :data="comments"
        border
        stripe
        @selection-change="handleSelectionChange"
        style="width: 100%; margin-top: 20px"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="id" label="评论ID" width="120" />
      <el-table-column prop="userId" label="用户ID" width="120" />
      <el-table-column prop="songId" label="歌曲ID" width="120" />
      <el-table-column prop="playlistId" label="歌单ID" width="120" />
      <el-table-column prop="content" label="评论内容" min-width="200" />
      <el-table-column prop="createTime" label="评论时间" width="180" />
      <el-table-column prop="type" label="评论类型" width="120">
        <template #default="scope">
          {{ scope.row.type === 0 ? '歌曲评论' : '歌单评论' }}
        </template>
      </el-table-column>
      <el-table-column prop="likeCount" label="点赞数" width="100" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button type="danger" size="small" @click="deleteComment(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentPageChange"
        :current-page="commentQuery.pageNum"
        :page-sizes="[10, 20, 50]"
        :page-size="commentQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px; text-align: right"
    />

    <!-- 批量删除确认弹窗 -->
    <el-dialog
        title="删除确认"
        v-model="deleteDialogVisible"
        width="30%"
    >
      <span>确定要删除选中的 {{ selectedCommentIds.length }} 条评论吗？</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="deleteDialogVisible = false">取消</el-button>
          <el-button type="danger" @click="confirmBatchDelete">确定删除</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import http from '@/request/http.js' // 替换为你的axios封装路径
import { ElMessage } from 'element-plus'

// 评论查询参数（对应CommentDTO）
const commentQuery = ref({
  pageNum: 1,
  pageSize: 10,
  content: '',
  type: ''
})

// 评论列表数据
const comments = ref([])

// 总记录数
const total = ref(0)

// 选中的评论ID集合
const selectedCommentIds = ref([])

// 删除确认弹窗显隐
const deleteDialogVisible = ref(false)

// 页面加载时初始化评论列表
onMounted(() => {
  fetchComments()
})

// 获取评论列表
const fetchComments = async () => {
  try {
    const res = await http.post('/admin/getComment', commentQuery.value)
    if (res.code === 0) {
      comments.value = res.data.items || []
      total.value = res.data.total || 0
    } else {
      ElMessage.error(res.message || '获取评论失败')
    }
  } catch (error) {
    console.error('获取评论接口异常:', error)
    ElMessage.error('网络异常，获取评论失败')
  }
}

// 处理表格选中项变化
const handleSelectionChange = (selected) => {
  selectedCommentIds.value = selected.map(item => item.id)
}

// 处理每页条数变化
const handleSizeChange = (size) => {
  commentQuery.value.pageSize = size
  commentQuery.value.pageNum = 1
  fetchComments()
}

// 处理当前页码变化
const handleCurrentPageChange = (page) => {
  commentQuery.value.pageNum = page
  fetchComments()
}

// 显示批量删除确认弹窗
const showDeleteConfirm = () => {
  deleteDialogVisible.value = true
}

// 确认批量删除
const confirmBatchDelete = async () => {
  try {
    const res = await http.delete('/admin/deleteComments', {
      data: selectedCommentIds.value // 注意：若后端不支持DELETE传body，需改为POST
    })
    if (res.code === 0) {
      ElMessage.success('批量删除成功')
      deleteDialogVisible.value = false
      fetchComments() // 刷新评论列表
    } else {
      ElMessage.error(res.message || '批量删除失败')
    }
  } catch (error) {
    console.error('批量删除接口异常:', error)
    ElMessage.error('网络异常，批量删除失败')
  }
}

// 单个删除评论
const deleteComment = async (commentId) => {
  try {
    const res = await http.delete('/admin/deleteComments', {
      data: [commentId] // 单个ID也用数组，适配批量接口
    })
    if (res.code === 0) {
      ElMessage.success('删除成功')
      fetchComments() // 刷新评论列表
    } else {
      ElMessage.error(res.message || '删除失败')
    }
  } catch (error) {
    console.error('删除评论接口异常:', error)
    ElMessage.error('网络异常，删除失败')
  }
}
</script>

<style scoped>
.comment-manage-page {
  padding: 20px;
  background-color: #fff;
  min-height: calc(100vh - 60px);
}

.search-filter-area {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}

.search-input {
  width: 240px;
}

.type-select {
  width: 160px;
}
</style>